package com.security.utils;

import com.security.exception.CustomException;
import com.security.exception.CustomExceptionType;
import lombok.Data;

@Data
public class R {

    private boolean isok;
    private int code;
    private String msg;
    private Object data;
    private long count;

    public R(){
        this.isok = true;
        this.code = 200;
        this.msg = "ok";
    }
    public R(Object data){
        this.isok = true;
        this.code = 200;
        this.msg = "ok";
        this.data = data;
    }
    public R(String msg){
        this.isok = true;
        this.code = 200;
        this.msg = msg;
    }
    public R(String msg, Object data){
        this.isok = true;
        this.code = 200;
        this.msg = msg;
        this.data = data;
    }
    public R(long count, Object data){
        this.isok = true;
        this.code = 0;
        this.msg = "ok";
        this.count = count;
        this.data = data;
    }
    public R(int code, String msg){
        this.isok = true;
        this.code = code;
        this.msg = msg;
    }
    public R(boolean isok, int code, String msg){
        this.isok = isok;
        this.code = code;
        this.msg = msg;
    }

    //请求异常时响应数据
    public static R error(CustomException e){
        R r = new R();
       r.setIsok(false);
        r.setCode(e.getCode());
        if (e.getCode() == CustomExceptionType.USER_INPUT_ERROR.getCode()){
            r.setMsg(e.getMessage());
        }else if(e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()){
            r.setMsg("系统异常，请联系管理员，电话：13877887788");
        }else{
            r.setMsg("未知异常，请联系管理员，电话：13877887788");
        }
        return r;
    }

    public static R error(int code, String msg){
        return new R(false,code,msg);
    }

    public static R success(){
        return new R();
    }

    public static R success(Object data){
        return new R(data);
    }

    public static R success(String msg){
        return new R(msg);
    }

    public static R success(String msg, Object data){
        return new R(msg, data);
    }

    public static R table(long count, Object data) {
       return new R(count,data);
    }
}
